<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'serch.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  按名字查询：
    <form action="" method=post>
    <input type="text" name="cname"/>
    <input type="submit" value="查询"/>
  
    </form>
    
    <% 	Connection con;
    	Statement sql; 
    	ResultSet rs;
    	String name;
    	request.setCharacterEncoding("UTF-8");
    	if(request.getParameter("cname")!=null&&request.getParameter("cname")!="")
       		name=(String)request.getParameter("cname");
    	else
       		name="";
       
    	try{  Class.forName("com.mysql.jdbc.Driver");
    		}
    	catch(Exception e){
       		out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
    		}
    		
    try { String uri= "jdbc:mysql://localhost:3306/peopletext";
          String user="root";
          String password="kamui5201";
          String str="";
          con=DriverManager.getConnection(uri,user,password);
          sql=con.createStatement();
          str="select * from pinfo WHERE pname like '%"+name+"%'";
          rs=sql.executeQuery(str);
          out.println(str);
          out.print("<table border=1>");
          	out.print("<tr>");
            	out.print("<th width=100>"+"姓名");
           	 	out.print("<th width=100>"+"电话");
            	out.print("<th width=100>"+"地址");
          	out.print("</tr>");
          	while(rs.next()){
            	out.print("<tr>");
              	out.print("<td>"+rs.getString(1)+"</td>"); 
              	out.print("<td>"+rs.getString(2)+"</td>");
              	out.print("<td>"+rs.getString(3)+"</td>"); 
              
           	 out.print("</tr>") ; 
          	}
          	out.print("</table>");
          con.close();
    }
    catch(SQLException e){ 
          out.print(e);
    }
 %>
  </body>
</html>
